/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events;

import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.GUIDStorage;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.AnonymousPawn;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.GenericPawn;

/**
 * @author Massimo Canonaco
 *
 */
public class MovementEvent implements GenericEvent 
{

	/**It's the character */
	private GenericPawn actor;
	
	/**It's the actual row value on which the actor has been moved.*/
	private int actualRow;
	
	/**It's the actual column value on which the actor has been moved.*/
	private int actualColumn;
	
	/**It's the previous value of the row. */
	private int frowRow;
	
	/**It's the previous value of the column. */
	private int fromColumn;
	
	/** */ 
	public static float GUID = GUIDStorage.MOVEMENT_EVENT;
	
	/** */
	public MovementEvent(GenericPawn actor, int aRow, int aCol, int fRow, int fCol)
	{
		frowRow = fRow;
		fromColumn = fCol;
		
		actualRow = aRow;
		actualColumn = aCol;
		
		this.actor = actor;
	}
	/* (non-Javadoc)
	 * @see it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events.GenericEvent#getGUID()
	 */
	@Override
	public float getGUID() {
		return GUID;
	}

	/* (non-Javadoc)
	 * @see it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events.GenericEvent#getName()
	 */
	@Override
	public String getName() 
	{
		return "movementEvent";
	}
	/**
	 * @return the actor
	 */
	public GenericPawn getActor() {
		return actor;
	}
	/**
	 * @return the oldRow
	 */
	public int getOldRow() {
		return frowRow;
	}
	/**
	 * @return the oldColumn
	 */
	public int getOldColumn() {
		return fromColumn;
	}
	/**
	 * @return the actualRow
	 */
	public int getActualRow() {
		return actualRow;
	}
	/**
	 * @return the actualColumn
	 */
	public int getActualColumn() {
		return actualColumn;
	}
	/**
	 * @return the frowRow
	 */
	public int getFrowRow() {
		return frowRow;
	}
	/**
	 * @param frowRow the frowRow to set
	 */
	public void setFrowRow(int frowRow) {
		this.frowRow = frowRow;
	}
	/**
	 * @return the fromColumn
	 */
	public int getFromColumn() {
		return fromColumn;
	}
	/**
	 * @param fromColumn the fromColumn to set
	 */
	public void setFromColumn(int fromColumn) {
		this.fromColumn = fromColumn;
	}
	/**
	 * @param actualRow the actualRow to set
	 */
	public void setActualRow(int actualRow) {
		this.actualRow = actualRow;
	}
	/**
	 * @param actualColumn the actualColumn to set
	 */
	public void setActualColumn(int actualColumn) {
		this.actualColumn = actualColumn;
	}
	@SuppressWarnings("unchecked")
	@Override
	public JSONObject getAsJSONObject() 
	{
		JSONObject object = new JSONObject();
		
		object.put(GLOBAL_GUID_KEY, GUID);
		
		JSONArray array = new JSONArray();
		array.add(actor.getRow());
		array.add(actor.getColumn());
		array.add(frowRow);
		array.add(fromColumn);
		array.add(actualRow);
		array.add(actualColumn);
		object.put(PAWN_POSITION_KEY, array);
		
		return object;
	}
	@Override
	public void setFromJSONOnbject(JSONObject object) 
	{
		actor = new AnonymousPawn();
		JSONArray array = (JSONArray) object.get(PAWN_POSITION_KEY);
		Iterator<Integer>iterator = array.iterator();
		
		actor.setPosition(iterator.next(), iterator.next());
		
		frowRow = iterator.next().intValue();
		fromColumn = iterator.next().intValue();
		actualRow = iterator.next().intValue();
		actualColumn = iterator.next().intValue();
	}

	/** */
	static private final String PAWN_POSITION_KEY = "pawnPosition";
}
